<template>
	<view class="body">
		<!-- 操作菜单 -->
		<paper-left-popup 
		:show="show" 
		@hide="hidePopup"
		@addFriend="addFriend"
		@cleanCache="cleanCache"></paper-left-popup>
		<!-- 小纸条列表 -->
		<block v-for="(item, index) in list" :key="index">
			<paper-list :item="item"></paper-list>
		</block>
		
		<!-- 上拉加载 -->
		<load-more v-if="list.length > 10" :loadtext="loadtext"></load-more>
	</view>
</template>

<script>
	import paperList from '../../components/paper/paper-list.vue';
	import loadMore from '../../components/common/load-more.vue';
	import paperLeftPopup from '../../components/paper/paper-left-popup.vue';
	export default {
		components:{
			paperList,
			loadMore,
			paperLeftPopup
		},
		data() {
			return {
				// 菜单显示状态
				show: false,
				loadtext: '上拉加载更多',
				list: [
					{
						avatar: '../../static/images/logo.png',
						nickname:'拜登',
						time:'12:00',
						msg: '特朗普，你小子生气不？嘿嘿',
						badgenum: 12
					},
					{
						avatar: '../../static/images/logo.png',
						nickname:'特朗普',
						time:'12:01',
						msg: '算你狠',
						badgenum: 1
					},
					{
						avatar: '../../static/images/logo.png',
						nickname:'奥巴马',
						time:'12:01',
						msg: '我就看看，嘻嘻',
						badgenum: 0
					},
					{
						avatar: '../../static/images/logo.png',
						nickname:'奥巴马',
						time:'12:01',
						msg: '我就看看，嘻嘻',
						badgenum: 0
					},
					{
						avatar: '../../static/images/logo.png',
						nickname:'奥巴马',
						time:'12:01',
						msg: '我就看看，嘻嘻',
						badgenum: 0
					},
					{
						avatar: '../../static/images/logo.png',
						nickname:'奥巴马',
						time:'12:01',
						msg: '我就看看，嘻嘻',
						badgenum: 0
					},
					{
						avatar: '../../static/images/logo.png',
						nickname:'奥巴马',
						time:'12:01',
						msg: '我就看看，嘻嘻',
						badgenum: 0
					},
					{
						avatar: '../../static/images/logo.png',
						nickname:'奥巴马',
						time:'12:01',
						msg: '我就看看，嘻嘻',
						badgenum: 0
					},
					{
						avatar: '../../static/images/logo.png',
						nickname:'奥巴马',
						time:'12:01',
						msg: '我就看看，嘻嘻',
						badgenum: 0
					}
				]
			}
		},
		/* 监听下拉刷新功能 */
		onPullDownRefresh() {
			this.pullDownGetData();
		},
		/* 滚动触底事件 */
		onReachBottom() {
			this.loadMore();
		},
		/* 监听导航按钮点击事件 */
		onNavigationBarButtonTap(e) {
			switch (e.index){
				case 0:
					//左边，跳转到好友列表
					this.navToFriendList();
					break;
				default:
					this.showPopup();
					break;
			}
		},
		methods: {
			// 跳转好友列表
			navToFriendList(){
				uni.navigateTo({
					url: '../user-list/user-list'
				})
			},
			// 下拉获取数据
			pullDownGetData(){
				// 服务器获取数据
				// 赋值
				// 停止刷新
				setTimeout(() => {
					let arr = [
						{
							avatar: '../../static/images/logo.png',
							nickname:'拜登',
							time:'12:00',
							msg: '特朗普，你小子生气不？嘿嘿',
							badgenum: 0
						},
						{
							avatar: '../../static/images/logo.png',
							nickname:'特朗普',
							time:'12:01',
							msg: '算你狠',
							badgenum: 11
						},
						{
							avatar: '../../static/images/logo.png',
							nickname:'奥巴马',
							time:'12:01',
							msg: '我就看看，嘻嘻',
							badgenum: 12
						}
					];
					this.list = arr;
					uni.stopPullDownRefresh();
				}, 2000);
			},
			// 上拉加载更多
			loadMore(){
				if(this.loadtext !== '上拉加载更多'){
					return;
				}
				// 修改加载状态
				this.loadtext = '加载中···';
				// 获取数据
				setTimeout(() => {
					var obj = {
						avatar: '../../static/images/logo.png',
						nickname:'奥巴马',
						time:'12:01',
						msg: '我就看看，嘻嘻',
						badgenum: 0
					};
					// 获取完成
					this.list.push(obj);
					this.loadtext = '上拉加载更多';
				}, 1000)
				
				
				// this.loadtext = '没有更多数据了';
			},
			/* 操作菜单 */
			hidePopup(){
				this.show = false;
			},
			showPopup(){
				this.show = true;
			},
			// 添加好友
			addFriend(){
				this.hidePopup();
				console.log('添加糗友');
			},
			// 清除缓存
			cleanCache(){
				this.hidePopup();
				console.log('清除缓存');
			}
		}
	}
</script>

<style>
	.body{
		padding: 0 20upx;
	}
	
</style>
